package org.ihelpuoo.lang;

import java.util.Iterator;
import java.util.Map;

import org.ihelpuoo.I;

public class MapUtils
{
	public static boolean equals(Map m1, Map m2)
	{
		if (m1 == m2)
			return true;
		if (null == m1 || null == m2)
			return false;
		if (m1.size() != m2.size())
			return false;
		Iterator it = m1.keySet().iterator();
		while (it.hasNext())
		{
			Object key = it.next();
			if (!m2.containsKey(key))
				return false;
			Object v1 = m1.get(key);
			Object v2 = m2.get(key);
			if (v1 == null && v2 == null)
				continue;
			if (v1 == null || v2 == null)
				return false;
			if (!v1.equals(v2))
				return false;
		}
		return true;
	}

	public static String dump(Map map)
	{
		if (null == map)
			return null;
		if (map.size() == 0)
			return "{!EMPTY MAP}";
		Iterator it = map.keySet().iterator();
		StringBuffer sb = new StringBuffer("{");
		Object key;
		while (it.hasNext())
		{
			key = it.next();
			sb.append(I.NEW_LINE).append(key).append(" : ").append(map.get(key));
			if (it.hasNext())
				sb.append(',');
		}
		sb.append('}');
		return sb.toString();
	}
}
